package com.hexl.ucode.controller;

import java.util.Arrays;
import java.util.List;

import com.hexl.common.utils.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.hexl.framework.aspectj.lang.annotation.Log;
import com.hexl.framework.aspectj.lang.enums.BusinessType;
import com.hexl.ucode.domain.CodeDemo;
import com.hexl.ucode.service.ICodeDemoService;
import com.hexl.framework.web.controller.BaseController;
import com.hexl.framework.web.domain.AjaxResult;
import com.hexl.common.utils.poi.ExcelUtil;
import com.hexl.framework.web.page.TableDataInfo;

/**
 * 代码生成示例Controller
 *
 * @author hexl
 * @date 2021-07-09
 */
@Controller
@RequestMapping("/ucode/demo")
public class CodeDemoController extends BaseController
{
    private String prefix = "ucode/demo";

    @Autowired
    private ICodeDemoService codeDemoService;

    @RequiresPermissions("ucode:demo:view")
    @GetMapping()
    public String demo()
    {
        return prefix + "/demo";
    }

    /**
     * 查询代码生成示例列表
     */
    @RequiresPermissions("ucode:demo:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CodeDemo codeDemo)
    {
        startPage();
        List<CodeDemo> list = codeDemoService.selectCodeDemoList(codeDemo);
        return getDataTable(list);
    }

    /**
     * 导出代码生成示例列表
     */
    @RequiresPermissions("ucode:demo:export")
    @Log(title = "代码生成示例", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CodeDemo codeDemo,String ids)
    {
        List<CodeDemo> list = null;
        if (StringUtils.isNotEmpty(ids)){
            list = codeDemoService.listByIds(Arrays.asList(ids.split(",")));
        }else {
            list = codeDemoService.selectCodeDemoList(codeDemo);
        }
        ExcelUtil<CodeDemo> util = new ExcelUtil<CodeDemo>(CodeDemo.class);
        return util.exportExcel(list, "代码生成示例数据");
    }

    /**
     * 新增代码生成示例
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存代码生成示例
     */
    @RequiresPermissions("ucode:demo:add")
    @Log(title = "代码生成示例", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CodeDemo codeDemo)
    {
        return toAjax(codeDemoService.insertCodeDemo(codeDemo));
    }

    /**
     * 修改代码生成示例
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        CodeDemo codeDemo = codeDemoService.selectCodeDemoById(id);
        mmap.put("codeDemo", codeDemo);
        return prefix + "/edit";
    }

    /**
     * 修改保存代码生成示例
     */
    @RequiresPermissions("ucode:demo:edit")
    @Log(title = "代码生成示例", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CodeDemo codeDemo)
    {
        return toAjax(codeDemoService.updateCodeDemo(codeDemo));
    }

    /**
     * 删除代码生成示例
     */
    @RequiresPermissions("ucode:demo:remove")
    @Log(title = "代码生成示例", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(codeDemoService.deleteCodeDemoByIds(ids));
    }
}
